/*
 * Isman Usoh<isman.usoh@gmail.com>
 * www.facebook.com/isman.usoh
 */
package com.isman.postcodeinfo.database;


import java.util.ArrayList;


import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class PostCodeDataSource {
	private SQLiteDatabase database;
	private PostCodeDatabaseHelper dbHelper;
	private Context mContext;

	public PostCodeDataSource(Context mContext) {
		this.mContext = mContext;
	}

	public void open() throws SQLException {
		if(dbHelper == null)
			dbHelper = new PostCodeDatabaseHelper(mContext);
		if(database == null)
			database = dbHelper.getReadableDatabase();
	}

	public void close() {
		dbHelper.close();
	}
	
	public ArrayList<PostCode> search(String str) {
		return this.search(str, 0, 10);
	}
	
	public ArrayList<PostCode> search(String str,int start,int end){
		ArrayList<PostCode> postCodes = new ArrayList<PostCode>();

		str = "%"+str+"%";
		Cursor cursor = database.query(
				PostCodeDatabaseHelper.TABLE_POSTCODE, 
				postcodeAllColumn, 
				postcodeSelect, 
				new String[]{str,str,str,str}, null, null, null, start+","+end);
		
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			PostCode comment = cursorToPostCode(cursor);
			postCodes.add(comment);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return postCodes;
	}

	public PostCode cursorToPostCode(Cursor cursor) {
		PostCode postCode = new PostCode();
		postCode.setId(cursor.getInt(cursor.getColumnIndex(PostCodeDatabaseHelper.POSTCODE_COLUMN_ID)));
		postCode.setAddress(cursor.getString(cursor.getColumnIndex(PostCodeDatabaseHelper.POSTCODE_COLUMN_ADDRESS)));
		postCode.setDistrict(cursor.getString(cursor.getColumnIndex(PostCodeDatabaseHelper.POSTCODE_COLUMN_DISTRICT)));
		postCode.setPostcodel(cursor.getString(cursor.getColumnIndex(PostCodeDatabaseHelper.POSTCODE_COLUMN_POSTCODE)));
		postCode.setProvince(cursor.getString(cursor.getColumnIndex(PostCodeDatabaseHelper.POSTCODE_COLUMN_PROVINCE)));
		postCode.setName(cursor.getString(cursor.getColumnIndex(PostCodeDatabaseHelper.POSTCODE_COLUMN_NAME)));
		postCode.setLlong(cursor.getDouble(cursor.getColumnIndex(PostCodeDatabaseHelper.POSTCODE_COLUMN_LONG)));
		postCode.setLlat(cursor.getDouble(cursor.getColumnIndex(PostCodeDatabaseHelper.POSTCODE_COLUMN_LAT)));
		return postCode;
	}
	
	
	
	private String[] postcodeAllColumn = {
			PostCodeDatabaseHelper.POSTCODE_COLUMN_ID,
			PostCodeDatabaseHelper.POSTCODE_COLUMN_ADDRESS,
			PostCodeDatabaseHelper.POSTCODE_COLUMN_DISTRICT,
			PostCodeDatabaseHelper.POSTCODE_COLUMN_PROVINCE,
			PostCodeDatabaseHelper.POSTCODE_COLUMN_POSTCODE,
			PostCodeDatabaseHelper.POSTCODE_COLUMN_NAME,
			PostCodeDatabaseHelper.POSTCODE_COLUMN_LONG,
			PostCodeDatabaseHelper.POSTCODE_COLUMN_LAT
			};
	
	private String postcodeSelect = 
			PostCodeDatabaseHelper.POSTCODE_COLUMN_ADDRESS + " LIKE ? OR " +
			PostCodeDatabaseHelper.POSTCODE_COLUMN_DISTRICT + " LIKE ? OR " +
			PostCodeDatabaseHelper.POSTCODE_COLUMN_PROVINCE + " LIKE ? OR " +
			PostCodeDatabaseHelper.POSTCODE_COLUMN_POSTCODE + " LIKE ? ";
	
}
